﻿using System.Collections.Generic;
using Common;
using Common.Download;
using DtoModels;
using Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Portal.Models;
using SqlSugar;
using ICacheService = Portal.Services.Cache.ICacheService;

namespace Portal.Controllers
{
    public class CustomerController : MyBaseController
    {
        private readonly SqlSugarClient _sqlSugarClient;
        private readonly IHostEnvironment _hostingEnvironment;
        private readonly ICacheService _cacheService;

        public CustomerController(SqlSugarClient sqlSugarClient, IHostEnvironment hostingEnvironment, ICacheService cacheService) : base(hostingEnvironment)
        {
            _sqlSugarClient = sqlSugarClient;
            _hostingEnvironment = hostingEnvironment;
            _cacheService = cacheService;
        }

        public IActionResult LoadAllCustomer(CustomerInfoReqDto customerInfoReqDto, string downloadFiled)
        {
            #region 测试缓存
            //var cacheKey = "hello";
            //var addResult = _cacheService.Add(cacheKey, downloadFiled);

            //if (addResult)
            //{
            //    var cacheValue = this._cacheService.Get(cacheKey);
            //}
            #endregion



            var querySqlQueryable = this._sqlSugarClient.Context.Queryable<bus_customer>();
            querySqlQueryable = querySqlQueryable.Where(customerInfoReqDto);

            if (string.IsNullOrEmpty(downloadFiled) == false)
            {
                var downloadField = JsonConvert.DeserializeObject<List<DownloadUtils.DownloadField>>(downloadFiled);

                var curDateTable = querySqlQueryable.ToDataTable();

                const string fileName = "客户端列表";
                var fileAllName = DownloadUtils.CombineFileAllPath(DownloadUtils.FolderPath.Excel, fileName, DownloadUtils.FileExtension.Xlsx);

                DownloadUtils.CreateNewExcelFile(fileName, "所有客户的信息", fileAllName, downloadField, curDateTable, null, null);

                var downloadResult = new
                {
                    code = 0,
                    msg = "查询成功",
                    data = DownloadUtils.GenerateDownloadUrl(Request, fileAllName)
                };

                return Json(downloadResult);
            }

            //分页查询逻辑
            var totalCount = 0;
            var queryResult = querySqlQueryable.ToPageList(customerInfoReqDto.Page, customerInfoReqDto.Limit, ref totalCount);

            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = totalCount,
                Data = queryResult
            };
            return Json(result);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="busCustomer"></param>
        /// <returns></returns>
        public IActionResult AddCustomer(bus_customer busCustomer)
        {
            var insertNum = this._sqlSugarClient.Insertable<bus_customer>(busCustomer).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功添加{insertNum}个用户"
            };

            return Json(result);
        }

        public IActionResult UpdateCustomer(bus_customer busCustomer)
        {
            var insertNum = this._sqlSugarClient.Updateable<bus_customer>(busCustomer).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功更新{insertNum}个用户"
            };

            return Json(result);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public IActionResult DeleteCustomer(int id)
        {
            var deleteNum = this._sqlSugarClient.Deleteable<bus_customer>(x => x.id == id).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除{deleteNum}个用户"
            };

            return Json(result);
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public IActionResult BatchDeleteCustomer(List<int> ids)
        {
            var deleteNum = this._sqlSugarClient.Deleteable<bus_customer>().In(ids).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除{deleteNum}个用户"
            };

            return Json(result);
        }

    }
}